{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Flask框架\n",
    "\n",
    "WSGI(Web服务器网关接口Python Web Server Gateway Interface，缩写为WSGI)提供的接口虽然比HTTP接口高级了不少，但和Web App的处理逻辑比，还是比较低级，我们需要在WSGI接口之上能进一步抽象，让我们专注于用一个函数处理一个URL，至于URL到函数的映射，就交给Web框架来做\n",
    "\n",
    "用Python开发一个Web框架十分容易，所以Python有上百个开源的Web框架。这里我们先不讨论各种Web框架的优缺点，直接选择一个比较流行的Web框架——Flask来使用\n",
    "\n",
    "\n",
    "### 项目--聊天室\n",
    "\n",
    "#### 第一步：启动服务器\n",
    "\n",
    "启动服务，进入主页，显示欢迎光临。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from flask import Flask\n",
    "\n",
    "app = Flask(__name__)\n",
    "\n",
    "\n",
    "@app.route(\"/\")\n",
    "def index():\n",
    "    return \"<h1>欢迎光临罗罗的网站</h1><p>Mdfsdf is AAAAAAAAAAAAAAAAA</p>\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * Serving Flask app \"__main__\" (lazy loading)\n",
      " * Environment: production\n",
      "   WARNING: This is a development server. Do not use it in a production deployment.\n",
      "   Use a production WSGI server instead.\n",
      " * Debug mode: off\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)\n",
      "127.0.0.1 - - [23/Jun/2021 09:50:35] \"\u001b[37mGET / HTTP/1.1\u001b[0m\" 200 -\n"
     ]
    }
   ],
   "source": [
    "app.run()  # 启动服务器"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 第二步：添加更多路由\n",
    "\n",
    "网站不可能只有一个页，下面我们增加不同的路由页面"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * Serving Flask app '__main__' (lazy loading)\n",
      " * Environment: production\n",
      "\u001b[31m   WARNING: This is a development server. Do not use it in a production deployment.\u001b[0m\n",
      "\u001b[2m   Use a production WSGI server instead.\u001b[0m\n",
      " * Debug mode: off\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)\n",
      "127.0.0.1 - - [05/Jan/2022 14:32:27] \"GET / HTTP/1.1\" 200 -\n",
      "127.0.0.1 - - [05/Jan/2022 14:32:28] \"GET /favicon.ico HTTP/1.1\" 200 -\n",
      "127.0.0.1 - - [05/Jan/2022 14:32:38] \"GET /louis/hello HTTP/1.1\" 200 -\n",
      "127.0.0.1 - - [05/Jan/2022 14:32:38] \"GET /favicon.ico HTTP/1.1\" 200 -\n"
     ]
    }
   ],
   "source": [
    "from flask import Flask\n",
    "\n",
    "app = Flask(__name__)\n",
    "\n",
    "\n",
    "@app.route(\"/\")\n",
    "def index():\n",
    "    \"\"\"首页\"\"\"\n",
    "    return \"<h1>欢迎光临</h1> <p> 发信息给某人可以这样: /USERNAME/MESSAGE </p>\"\n",
    "\n",
    "@app.route(\"/<username>\")\n",
    "def user(username):\n",
    "    return \"你好!\" + username\n",
    "\n",
    "\n",
    "@app.route(\"/<username>/<message>\")\n",
    "def send_message(username, message):\n",
    "    \"\"\"通过路由设置:/人名称/信息内容 来实现发送信息给某人\"\"\"\n",
    "    return \"{0}: {1}\".format(username, message)\n",
    "\n",
    "app.run()  # 启动服务器"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 第三步：保存信息\n",
    "\n",
    "刚才我们发的消息都没有保存起来，下次打开网站消息就没有，怎么能聊天呢？ 没有保存信息，我怎么能看到信息呢？所有一定要找个地方把信息保存起来。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from flask import Flask, redirect\n",
    "\n",
    "app = Flask(__name__)\n",
    "messages = [] # 保存聊天信息到列表\n",
    "\n",
    "\n",
    "def add_messages(username, message):\n",
    "    # 添加消息，保存在列表里面\n",
    "    messages.append(\"{}: {}\".format(username, message))\n",
    "\n",
    "def get_all_messages():\n",
    "    \"\"\"获取关于我的所有聊天信息`\"\"\"\n",
    "    return \"<br>\".join(messages)\n",
    "\n",
    "@app.route(\"/\")\n",
    "def index():\n",
    "    \"\"\"首页\"\"\"\n",
    "    return \"<h1>欢迎光临</h1> <p> 发信息给某人可以这样: /USERNAME/MESSAGE </p>\"\n",
    "\n",
    "\n",
    "@app.route(\"/<username>\")\n",
    "def user(username):\n",
    "    \"\"\"显示所有消息\"\"\"\n",
    "    return \"<h1>欢迎, {0}</h1>{1}\".format(username, get_all_messages())\n",
    "\n",
    "\n",
    "@app.route(\"/<username>/<message>\")\n",
    "def send_message(username, message):\n",
    "    \"\"\"\n",
    "    通过路由设置:/人名称/信息内容 来实现发送信息给某人\n",
    "    添加信息到列表里面，然后返回到用户页面\n",
    "    \"\"\"\n",
    "    add_messages(username, message)\n",
    "    return redirect(\"/\" + username)\n",
    "\n",
    "app.run() # 启动服务器"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
